<?php
/**
 * Created by PhpStorm.
 * User: wxy
 * Date: 2018/11/13
 * Time: 13:59
 */

namespace frontend\modules\resource\logic;

use common\models\HlSpecial;
use common\models\HyResource;
use common\models\HySpecialResource;
use common\models\HySupport;
use frontend\modules\common\models\BaseHelper;
use Yii;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;


class SpecialLogic
{
    /**
     * @param $id
     * @param $branchId
     * @return int
     * @throws \yii\base\ExitException
     */
    public function RemoveBranchSpecial($id, $branchId)
    {
        if (empty($id))
            BaseHelper::Error('id 不能为空');
        //从关联表移除
        $res = HySpecialResource::deleteAll(['id' => $id, 'br_id' => $branchId]);
        return $res;
    }

    /**
     * @param $id
     * @param $branchId
     * @return int
     * @throws \yii\base\ExitException
     */
    public function RemoveBranchSupport($id, $branchId)
    {
        if (empty($id))
            BaseHelper::Error('id 不能为空');
        //从关联表移除
        $res = HySupport::deleteAll(['id' => $id, 'br_id' => $branchId]);
        return $res;
    }

    /**
     * @param $id
     * @param $branchId
     * @throws \yii\base\ExitException
     */
    public function checkSupportResource($id, $branchId)
    {
        $ids = HySupport::find()->select(['res_id', 'res_type'])
            ->where(['id' => $id])
            ->asArray()
            ->all();
        if (empty($ids))
            BaseHelper::Error('没有找到资源');
        foreach ($ids as $key => $val) {
            $model = (new ResourceLogic())->getResourceModel($val['res_type']);
            $num = $model->find()
                ->where(['res_id' => $val['res_id']])
                ->andWhere(['is_self' => $branchId])
                ->count();
            if (empty($num))
                BaseHelper::Error('不能操作系统分配资源');
        }
    }

    /**
     * @param $id
     * @param $branchId
     * @throws \yii\base\ExitException
     */
    public function checkBranchSpecialResource($id, $branchId)
    {
        $ids = HySpecialResource::find()->select(['res_id', 'type'])
            ->where(['id' => $id])
            ->asArray()
            ->all();
        if (empty($ids))
            BaseHelper::Error('没有找到资源');
        foreach ($ids as $key => $val) {
            $model = (new ResourceLogic())->getResourceModel($val['type']);
            $num = $model->find()
                ->where(['res_id' => $val['res_id']])
                ->andWhere(['is_self' => $branchId])
                ->count();
            if (empty($num))
                BaseHelper::Error('不能操作系统分配资源');
        }
    }

    /**
     * @param ActiveRecord $model
     * @param $branchId
     * @return bool
     * @throws \yii\base\ExitException
     */
    public function saveSpecialInfo(ActiveRecord $model, $branchId, $is_self = 0)
    {

        $data = Yii::$app->request->post();
        unset($data['_csrf-frontend']);
        $data = array_shift($data);
        if (empty($data))
            BaseHelper::Error('数据有误');

        $data['br_id'] = $branchId;
        if ($is_self) {
            $data['is_self'] = $is_self;
        }

        if ($model->load($data, '') && $model->save()) {
            return true;
        }
        return false;
    }


    /**
     * 专题列表
     * @param int $branchId
     * @return array
     */
    public function specialMap($branchId = 0)
    {
        $model = (new HlSpecial())->find()
            ->select('id,title')
            ->where(['status' => 1]);
        if ($branchId) {
            $model->andWhere(['br_id' => $branchId, 'is_self' => $branchId]);
        }
        $res = $model->asArray()
            ->all();

        $data = ArrayHelper::map(array_merge($res), 'id', 'title');
        return $data;
    }

    /**
     * @param $id
     * @param $branchId
     * @return bool
     * @throws \yii\base\ExitException
     */
    public function UpdateSupport($id, $branchId)
    {

        if (!is_numeric($id))
            BaseHelper::Error('参数格式有误');
        $model = HlSpecial::find()->where(['id' => $id, 'br_id' => $branchId])->one();
        if (empty($model))
            BaseHelper::Error('没有找到对应记录');
        $model->support = !$model->support;
        return $model->save(false);
    }


    /**
     * @param $id
     * @param $branchId
     * @throws \yii\base\ExitException
     */
    public function checkSId($id, $branchId)
    {
        $num = HlSpecial::find()->where(['id' => $id, 'br_id' => $branchId, 'is_self' => $branchId])->count();
        if (empty($num))
            BaseHelper::Error('不能操作系统分配资源');

    }
}